// pages/cart/orderSubmit/orderSubmit.js
Page({

	data: {
		foods:[],
		totalPrice:0,
		expressPrice:5,
		time:'11:00',
		couponList:[],
		couponSelected:-1,
		hideCouponSelector:false
	},
	onLoad: function (options) {
		//加载优惠券
		wx.request({
			url: 'http://127.0.0.1:7080/coupon',
			header: {
				'Content-Type': 'application/json'
			},
			success: (res) => {
				this.setData({
					couponList:res.data
				})
			}
		})
		//加载商品数据
		wx.getStorage({
			key: 'orderGoods',
			success: (res) => {
				let foods = JSON.parse(res.data)
				this.setData({ foods })
				// 计算总价,和配送费
				let totalPrice = foods.reduce((total,cur)=>{
					return total += cur.price*cur.count
				},0)
				this.setData({totalPrice})
				totalPrice > 47 && this.setData({expressPrice:0})
			} 
		})
	},
	onReady: function () {

	},
	onShow: function () {
		
	},
	//////////////////////////*********** */custom methods*******////////////////////////////
	//显示时间选择
	bindTimeChange(event){
		console.log(event.detail);
		this.setData({time:event.detail.value})
	},
	//切换优惠券选择
	toggleCouponSelect(){
		this.setData({
			hideCouponSelector:!this.data.hideCouponSelector
		})
	},
	//显示优惠券选择器
	showCouponSelect(){
		this.setData({ hideCouponSelector:false })
	},
	//选择优惠券
	chooseCoupon(e){
		let idx = e.currentTarget.dataset.idx
		let priceLimit = this.data.couponList[idx].priceLimit
		if(priceLimit < this.data.totalPrice){
			this.setData({couponSelected:idx})
		}else{
			wx.showToast({title:'不满条件！',duration:800})
		}
	}
})